package com.atguigu.champter5.Flink05.transformation.UDF;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class RichFuction {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.fromElements(1,2,3,4)
                .map(new RichMapFunction<Integer, Integer>() {
                    @Override
                    public void open(Configuration parameters) throws Exception {
                        // 一般可以在open方法中 创建一个数据库的连接
                        // 算子的并行度为几， 那么 open方法就会执行几次。close跟open类似
                        System.out.println("RichFuction.open");

                    }

                    @Override
                    public void close() throws Exception {
                        // 一般可以在close方法中 可以归还一个数据库连接 做一些清理工作 关闭MySQL数据库的连接
                        System.out.println("RichFuction.close");

                    }

                    @Override
                    public Integer map(Integer value) throws Exception {
                        System.out.println("RichFuction.map");
                        return value * value;

                    }
                }).print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
